-- Client Side --

CustomMapsList = {}
CustomMaps = {}


-- Team Delete Notice (Client)
function apple_tdm_teamdeletenotice(data)
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'Your team was deleted, rejoining a team!')
end
usermessage.Hook("apple_tdm_teamdeletenotice", apple_tdm_teamdeletenotice)

-- Can not kill to switch teams faster, sorry!
function TDM_Can_Kill_Self(data)
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),']',Color(255,255,255,255),' Sorry, but you can not kill yourself!')
end
usermessage.Hook("TDM_Can_Kill_Self", TDM_Can_Kill_Self)

-- Double Kill
function TDM_Player_IS_DK(data)
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),']',Color(255,255,255,255),' Double Kill!')
	surface.PlaySound( "apptdm/doublekill.mp3" )
end
usermessage.Hook("TDM_Player_IS_DK", TDM_Player_IS_DK)


-- Triple Kill
function TDM_Player_IS_TK(data)
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),']',Color(255,255,255,255),' Triple Kill!')
	surface.PlaySound( "apptdm/triplekill.mp3" )
end
usermessage.Hook("TDM_Player_IS_TK", TDM_Player_IS_TK)


-- Killing Spree
function TDM_Player_IS_KS(data)
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),']',Color(255,255,255,255),' Killing Spree!')
	surface.PlaySound( "apptdm/killingspree.mp3" )
end
usermessage.Hook("TDM_Player_IS_KS", TDM_Player_IS_KS)


-- Killtacular
function TDM_Player_IS_KC(data)
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),']',Color(255,255,255,255),' Kill-Fucking-Tacular!')
	surface.PlaySound( "apptdm/killtacular.mp3" )
end
usermessage.Hook("TDM_Player_IS_KC", TDM_Player_IS_KC)

-- Not an admin
function apple_tdm_notadmin(data)
	surface.PlaySound( "apptdm/error.mp3" )
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'You are not an admin!')
--	Derma_Message("You are not an admin!", "[TDM] Error", "OK")
end
usermessage.Hook("apple_tdm_notadmin", apple_tdm_notadmin)

-- First Blood
function TDM_Player_IS_FK(data)
	surface.PlaySound( "apptdm/firstblood.mp3" )
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),']',Color(255,255,255,255),' First Blood!')
end
usermessage.Hook("TDM_Player_IS_FK", TDM_Player_IS_FK)

-- First Blood To Everyone
function TDM_Players_IS_FK(data)
local GetKilName = data:ReadString()
local GetKilTeam = data:ReadShort()
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',team.GetColor(GetKilTeam),GetKilName,Color(255,255,255,255),' has received first blood!')
end
usermessage.Hook("TDM_Players_IS_FK", TDM_Players_IS_FK)


-- Sends Points At Join
function apple_tdm_sendnewpointsj(data)
	local PlayerPointsByXPID = data:ReadString()
	local PlayerPointsByXP = data:ReadString()
	if player.GetByID(PlayerPointsByXPID):IsValid() then
		player.GetByID(PlayerPointsByXPID):SetPData("CurrentTDMPoints2", PlayerPointsByXP)
	end
end
usermessage.Hook("apple_tdm_sendnewpointsj", apple_tdm_sendnewpointsj)
function apple_tdm_sendnewpointsjj(data)
	local PlayerPointsByXPID = data:ReadString()
	local PlayerPointsByXPName = data:ReadString()
	if player.GetByID(PlayerPointsByXPID):IsValid() then
		player.GetByID(PlayerPointsByXPID):SetPData("TDMRankNameUp", PlayerPointsByXPName)
	end
end
usermessage.Hook("apple_tdm_sendnewpointsjj", apple_tdm_sendnewpointsjj)


net.Receive( "apple_tdm_customentitiesr5", function() 
CustomEntites = {} 
for k, v in pairs(net.ReadTable()) do
	CustomEntites[k] = {
			id = v.ID,
			name = tostring(v.Name2),
			xpos = tostring(v.XPos),
			ypos = tostring(v.YPos),
			zpos = tostring(v.ZPos)
	}
end
end)


net.Receive( "TDMTDMListMap2", function() 
CustomMapsList = {} 
for k, v in pairs(net.ReadTable()) do
	CustomMapsList[k] = {
			name = tostring(v.Name2)
	}
end
end)


net.Receive( "apple_tdm_customemaps", function() 
CustomMaps = {} 
CustomMaps = net.ReadTable()
--PrintTable(CustomMaps)
end)


-- Team Delete Notice (Client)
function apple_tdm_editnotice(data)
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'Your team has been edited!')
end
usermessage.Hook("apple_tdm_editnotice", apple_tdm_editnotice)


-- Team Won
function apple_tdm_who_won(data)
local GetTeam = data:ReadShort()
local GetScoreN = data:ReadShort()
local NewMapName = data:ReadString()
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'Team: ',team.GetColor(GetTeam),team.GetName(GetTeam),Color(255,255,255,255)," has won this game with the score of ",Color(255,0,0,255),tostring(GetScoreN))
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'The server will now switch to ',Color(255,0,0,255),NewMapName,Color(255,255,255,255),' in ',Color(255,0,0,255),'30',Color(255,255,255,255),' seconds!')
	timer.Simple(5, function()
		chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,0,0,255),'25 ',Color(255,255,255,255),'seconds remaining!')
	end)
	timer.Simple(10, function()
		chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,0,0,255),'20 ',Color(255,255,255,255),'seconds remaining!')
	end)
	timer.Simple(15, function()
		chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,0,0,255),'15 ',Color(255,255,255,255),'seconds remaining!')
	end)
	timer.Simple(20, function()
		chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,0,0,255),'10 ',Color(255,255,255,255),'seconds remaining!')
	end)
	timer.Simple(25, function()
		chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,0,0,255),'5 ',Color(255,255,255,255),'seconds remaining!')
	end)
	timer.Simple(29, function()
		chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,0,0,255),'1 ',Color(255,255,255,255),'second remaining!')
	end)
end
usermessage.Hook("apple_tdm_who_won", apple_tdm_who_won)


-- Reloaded Entites
function apple_tdm_reloadentites(data)
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'You have reloaded all the entities!')
end
usermessage.Hook("apple_tdm_reloadentites", apple_tdm_reloadentites)


-- Created Entites
function apple_tdm_entycre(data)
local ID = data:ReadString()
local Name = data:ReadString()
local XPos = data:ReadString()
local YPos = data:ReadString()
local ZPos = data:ReadString()
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'You have created entity: ',Color(255,0,0,255),Name,Color(255,255,255,255),"[",Color(255,0,0,255),ID,Color(255,255,255,255),"] at ", Color(255,0,0,255), tostring(XPos), Color(255,255,255,255), ',',Color(255,0,0,255), tostring(YPos), Color(255,255,255,255), ',', Color(255,0,0,255), tostring(ZPos), Color(255,255,255,255), '!')
end
usermessage.Hook("apple_tdm_entycre", apple_tdm_entycre)


-- Deleted Entites
function apple_tdm_deletentity(data)
local GetName = data:ReadString()
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'You have deleted entity: ',Color(255,0,0,255),GetName)
end
usermessage.Hook("apple_tdm_deletentity", apple_tdm_deletentity)


-- Reloaded Entites All
function apple_tdm_reloadentitesa(data)
local Team = data:ReadShort()
local GetName = data:ReadString()
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',team.GetColor(Team),GetName,Color(255,255,255,255),' has reloaded all the entities!')
end
usermessage.Hook("apple_tdm_reloadentitesa", apple_tdm_reloadentitesa)


-- First Join Notice
function apple_tdm_welcometoserver(data)
	local GetHost = data:ReadString()
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'Welcome to ',GetHost,'!')
	surface.PlaySound( "apptdm/njoin1.mp3" )
	surface.PlaySound( "apptdm/njoin2.mp3" )
end
usermessage.Hook("apple_tdm_welcometoserver", apple_tdm_welcometoserver)


-- First Team Notice
function apple_tdm_youhavejoined(data)
	local ID = data:ReadShort()
	local Teamname = data:ReadString()
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'You have joined team ',team.GetColor(ID),Teamname,Color(255,255,255,255),'!')
end
usermessage.Hook("apple_tdm_youhavejoined", apple_tdm_youhavejoined)


-- Attempted to Change Model to Invalid Model
function apple_tdm_attemptmodelfail(data)
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'Do not try to cheat a non valid model!')
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'You will now be kicked as punishment!')
end
usermessage.Hook("apple_tdm_attemptmodelfail", apple_tdm_attemptmodelfail)


-- Attempted to Change Model to Invalid Model
function apple_tdm_changeteam(data)
local ID = data:ReadShort()
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'You have switched to team ',team.GetColor(ID),team.GetName(ID),Color(255,255,255,255),'!')
end
usermessage.Hook("apple_tdm_changeteam", apple_tdm_changeteam)


-- Goes to pos
function apple_tdm_createdpos(data)
local XPos = data:ReadShort()
local YPos = data:ReadShort()
local ZPos = data:ReadShort()
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'You have gone to spawn: ', Color(255,0,0,255), tostring(XPos), Color(255,255,255,255), ',',Color(255,0,0,255), tostring(YPos), Color(255,255,255,255), ',', Color(255,0,0,255), tostring(ZPos), Color(255,255,255,255), '!')
end
usermessage.Hook("apple_tdm_wentpos", apple_tdm_createdpos)


-- Goes to pos entity
function apple_tdm_createdpose(data)
local XPos = data:ReadShort()
local YPos = data:ReadShort()
local ZPos = data:ReadShort()
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'You have gone to entity: ', Color(255,0,0,255), tostring(XPos), Color(255,255,255,255), ',',Color(255,0,0,255), tostring(YPos), Color(255,255,255,255), ',', Color(255,0,0,255), tostring(ZPos), Color(255,255,255,255), '!')
end
usermessage.Hook("apple_tdm_wentpose", apple_tdm_createdpose)


-- Adds pos
function apple_tdm_createdpos(data)
local TeamID = data:ReadShort()
local XPos = data:ReadShort()
local YPos = data:ReadShort()
local ZPos = data:ReadShort()
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'You have created spawn: ', Color(255,0,0,255), tostring(XPos), Color(255,255,255,255), ',',Color(255,0,0,255), tostring(YPos), Color(255,255,255,255), ',', Color(255,0,0,255), tostring(ZPos), Color(255,255,255,255), ' for team ',team.GetColor(TeamID),team.GetName(TeamID),Color(255,255,255,255),"!")
end
usermessage.Hook("apple_tdm_createdpos", apple_tdm_createdpos)

-- Deletes pos
function apple_tdm_deletepos(data)
local TeamID = data:ReadShort()
local XPos = data:ReadShort()
local YPos = data:ReadShort()
local ZPos = data:ReadShort()
	chat.AddText(Color(255,255,255,255), '[',Color(255,0,0,255), 'TDM',Color(255,255,255,255),'] ',Color(255,255,255,255),'You have deleted spawn: ', Color(255,0,0,255), tostring(XPos), Color(255,255,255,255), ',',Color(255,0,0,255), tostring(YPos), Color(255,255,255,255), ',', Color(255,0,0,255), tostring(ZPos), Color(255,255,255,255), ' for team ',team.GetColor(TeamID),team.GetName(TeamID),Color(255,255,255,255),"!")
end
usermessage.Hook("apple_tdm_deletepos", apple_tdm_deletepos)